﻿Public Class TrxPenggunaanStokPage
    Private Sub getnotrx()
        txtnopo.Text = ""
        Dim dt As New DataTable
        If Utils.GetFunction("getidtrx", New Object() {"ps"}, dt) Then
            If Utils.RowExist(dt) Then
                txtnopo.Text = dt(0)(0)
            End If
        End If
    End Sub
    Private Sub getGudang()
        cmbgudang.Items.Clear()
        Dim dt As New DataTable
        dt = Utils.SearchAllLike("rsitemlocation", "itemlocation", "")
        If Utils.RowExist(dt) Then

            For Each r As DataRow In dt.Rows
                cmbgudang.Items.Add(r("itemlocation").ToString)
                cmbgudang.SelectedIndex = 0
            Next
        End If
    End Sub
    Private Sub TrxPenggunaanStokPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getnotrx()
        GridTransaksi.Rows.Clear()
        dtpTrans.Value = Now
        getGudang()
    End Sub
    Private Sub sumtotal()
        'count item
        If GridTransaksi.Rows.Count > 0 Then
            Dim i As Integer = 0
            Dim d As Double = 0
            i = GridTransaksi.Rows.GetLastRow(DataGridViewElementStates.None)
            If GridTransaksi.Rows(i).IsNewRow Then
                txtTotalItem.Text = GridTransaksi.Rows.Count - 1
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count - 1
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            Else
                txtTotalItem.Text = GridTransaksi.Rows.Count
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            End If
        ElseIf GridTransaksi.Rows.Count = 0 Then
            txtTotalItem.Text = "0"
            txtTotal.Text = "0"
        End If

    End Sub
    Private Function GetAllEqual(ByVal tblname As String, ByVal finder As String, ByVal finderkey As String) As DataTable
        Dim dt As New DataTable
        Dim params As Object()
        Params = New Object() {"*", tblname, finder, finderkey}
        If Utils.executeSP("proc_searchallEqual", Params, dt) Then

        End If
        Return dt
    End Function

    Private Sub GridTransaksi_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridTransaksi.CellEndEdit
        If GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    'blm kelar=? masukkan search equal
                    Dim dt As New DataTable
                    dt = GetAllEqual("vitemnonpersediaan", "iditem", GridTransaksi.CurrentCell.Value)
                    If Utils.RowExist(dt) Then
                        GridTransaksi(1, e.RowIndex).Value = IIf(IsDBNull(dt(0)("namaitem")), "", dt(0)("namaitem"))
                        GridTransaksi(2, e.RowIndex).Value = IIf(IsDBNull(dt(0)("hargajual")), "", dt(0)("hargajual"))
                        GridTransaksi(4, e.RowIndex).Value = IIf(IsDBNull(dt(0)("satuanjual")), "", dt(0)("satuanjual"))
                        GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                        sumtotal()

                    Else
                        MsgBox("data tidak terdaftar")
                        GridTransaksi.Rows.RemoveAt(e.RowIndex)
                        GridTransaksi.CurrentCell = GridTransaksi.Item(0, e.RowIndex)
                        GridTransaksi.Item(0, e.RowIndex).Selected = True
                    End If
                End If
            End If
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                If dgr.Index <> GridTransaksi.CurrentCell.RowIndex Then
                    If dgr.Cells(0).Value = GridTransaksi.CurrentCell.Value Then
                        dgr.Cells(3).Value = dgr.Cells(3).Value + 1
                        dgr.Cells(5).Value = dgr.Cells(3).Value * dgr.Cells(2).Value

                        GridTransaksi.Rows.Remove(GridTransaksi.CurrentRow)
                        sumtotal()

                    End If
                End If

            Next
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                    sumtotal()

                End If
            End If
        End If
    End Sub
    Private Sub GridTransaksi_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles GridTransaksi.EditingControlShowing
        If GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''ElseIf GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            ' ''    Dim TextBox1 As TextBox
            ''    TextBox1 = CType(e.Control, TextBox)
            ''    RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''    AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
        End If
    End Sub
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = Utils.HanyaAngka(Asc(e.KeyChar))
    End Sub
    Private Function validasirowgrid(ByVal dgr As DataGridView) As Boolean
        Dim retval As Boolean = True
        If GridTransaksi.RowCount > 0 Then
            If GridTransaksi.RowCount = 1 And GridTransaksi.Rows(0).IsNewRow Then
                retval = False
            End If
        Else
            retval = False
        End If
        Return retval


    End Function
    Private Sub GridTransaksi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyUp
        Dim ic, ir As Integer
        If validasirowgrid(GridTransaksi) Then
            ic = GridTransaksi.CurrentCell.ColumnIndex
            ir = GridTransaksi.CurrentCell.RowIndex
        Else
            Exit Sub
        End If

        If e.KeyValue = 13 Then
            If GridTransaksi.CurrentCell.ColumnIndex = 1 Or GridTransaksi.CurrentCell.ColumnIndex = 2 Then
                If Not IsDBNull(GridTransaksi(0, ir).Value) Or Not IsDBNull(GridTransaksi.CurrentCell.Value) Then
                    If Not GridTransaksi(0, ir).Value = "" Or Not GridTransaksi(1, ir).Value = "" Then



                        If Not IsDBNull(GridTransaksi(3, ir).Value) Then
                            If GridTransaksi(3, ir).Value = 0 Then
                                GridTransaksi(3, ir).Value = 1
                                GridTransaksi(5, ir).Value = GridTransaksi(3, ir).Value * GridTransaksi(2, ir).Value
                                sumtotal()
                                'sumhutang()
                                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                                GridTransaksi.CurrentCell.Selected = True
                            ElseIf GridTransaksi(3, ir).Value > 0 Then

                                GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                                GridTransaksi.CurrentCell.Selected = True
                            End If
                        Else
                            GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                            GridTransaksi.CurrentCell.Selected = True
                        End If

                    Else
                        GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        GridTransaksi.CurrentCell.Selected = True
                    End If
                Else
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                    GridTransaksi.CurrentCell.Selected = True
                End If

                'Dim eKey As New KeyEventArgs(Keys.Return)
                'GridTransaksi_KeyUp(sender, eKey)
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Or GridTransaksi.CurrentCell.ColumnIndex = 5 Then
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                GridTransaksi.CurrentCell.Selected = True
            End If
        End If
    End Sub

    Private Sub GridTransaksi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyDown
        If e.KeyValue = Keys.Delete Then
            Try
                Dim dgr As New DataGridViewRow
                dgr = GridTransaksi.CurrentRow
                GridTransaksi.Rows.Remove(dgr)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub GridTransaksi_RowsRemoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles GridTransaksi.RowsRemoved
        sumtotal()

    End Sub

    Private Sub AktifgridToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AktifgridToolStripMenuItem.Click
        GridTransaksi.Focus()
    End Sub

    Private Sub CariitemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CariitemToolStripMenuItem.Click
        Dim DialogSearchItem As New DialogSearchItem
        DialogSearchItem.TableKategori = "vitemnonpersediaan"
        DialogSearchItem.ShowDialog()
        Dim dgr As New DataGridViewRow
        dgr = DialogSearchItem.ReturnValue
        If dgr Is Nothing Then
            Exit Sub
        End If
        Dim str As String
        str = IIf(IsDBNull(dgr.Cells(0).Value), "", dgr.Cells(0).Value)
        If str = String.Empty Then
            Exit Sub
        Else
            GridTransaksi.Rows.Add()
            GridTransaksi(0, GridTransaksi.RowCount - 2).Value = str
            'GridTransaksi(1, GridTransaksi.RowCount - 2).Value = IIf(IsDBNull(dgr.Cells(1).Value), "", dgr.Cells(1).Value)
            'GridTransaksi(3, GridTransaksi.RowCount - 2).Value = 1
            GridTransaksi.Focus()
            GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 2)
            GridTransaksi(0, GridTransaksi.RowCount - 2).Selected = True
            GridTransaksi.BeginEdit(True)
            GridTransaksi.EndEdit(True)
        End If
    End Sub

    Private Sub FocusupplierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FocusupplierToolStripMenuItem.Click
        cmbgudang.Focus()
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click

        GridTransaksi.Rows.Clear()
        'setCombo()
        getnotrx()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        Dim retval As Boolean = True
        If cmbgudang.Text = "" Then
            retval = False
        ElseIf GridTransaksi.RowCount - 1 < 1 Then
            retval = False
        End If
        If Not retval Then
            MsgBox("Maaf Data Entry Tidak Valid")
            Exit Sub
        End If
        Dim retvalarr As New List(Of String)
        Dim dtstok As New DataTable
        Dim vstok As Double
        retvalarr.Clear()
        For Each dgr As DataGridViewRow In GridTransaksi.Rows
            dtstok = New DataTable
            If Not dgr.IsNewRow Then
                If Not dgr.Cells("qty").ReadOnly Then
                    If Utils.GetFunction("getstokitem", New Object() {CInt(dgr.Cells("iditem").Value), cmbgudang.Text}, dtstok) Then
                        If Utils.RowExist(dtstok) Then
                            vstok = dtstok(0)(0)
                            If vstok >= 0 Then
                                If vstok < dgr.Cells("qty").Value Then
                                    retvalarr.Add(dgr.Cells("namaitem").Value & " -> " & "Stok " & vstok & _
                                                  " Tidak Mencukupi")
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        Next
        If retvalarr.Count > 0 Then
            Dim retmsg As String = ""
            For Each smsg As String In retvalarr
                retmsg &= vbCrLf & smsg
            Next
            MsgBox("Stok Menu Tidak Mencukupi" & retmsg)
            Exit Sub
        End If

        getnotrx()


        Dim dt As New DataTable
        Dim str As String = ""

        'If Utils.RowExist(dt) Then

        str = "Execute Finish"
        'End If
        Dim strdata As String = ""
        strdata = Utils.GridValueColumnToText(GridTransaksi, New Object() {"iditem", "namaitem", "harga", "qty", "satuan", "total"})
        Dim params As String()

        If Not strdata = "" Then
            params = strdata.Split("|")
            For int As Integer = 0 To params.Length - 1
                params(int) = params(int).Insert(0, cmbgudang.Text & ",")
                params(int) = params(int).Insert(0, dtpTrans.Value.ToString("yyyy-MM-dd") & ",")
                params(int) = params(int).Insert(0, txtnopo.Text & ",")
                params(int) = params(int).Insert(params(int).Length, "," & LocalSession.operatorname)

            Next
            strdata = String.Join("|", params)
            If strdata.EndsWith("|") Then
                strdata = LSet(strdata, strdata.Length - 1)
            End If
            Dim dtretval As New DataTable
            If Utils.executeSP("proc_penggunaanstok", New Object() {txtnopo.Text, GridTransaksi.RowCount - 1, 10, strdata}, dtretval) Then
                If Utils.RowExist(dtretval) Then
                    str = dtretval(0)(0)
                End If
                If Utils.exec_SP("proc_setstokexec", New Object() {txtnopo.Text}) Then

                End If
                MsgBox(str)
                If Not dtretval(0)(1) = 4 Then
                    GridTransaksi.Rows.Clear()
                    getnotrx()
                End If
            End If
        End If

    End Sub

    Private Sub TrxPenggunaanStokPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        getnotrx()
        GridTransaksi.Rows.Clear()
        dtpTrans.Value = Now
        getGudang()
    End Sub
End Class
